<?php
namespace Home\Controller;


class IndexController extends \Common\Controller\BaseController {
    public function index(){
        //var_dump(session('username'));
		//标签生成
        $db = M('tab');
        $tags = $db->select();
        $this->assign('tags',$tags);
		
        $db = M('list');
        $list = $db->limit(5)->order('time desc')->select();
        $sum = $db->count();
        //var_dump($list);
		
		$count = 0;
		//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
		foreach($list as $k => $v){
			$separator = ',';
			$string = $v['type'];
			$array=explode($separator,$string);
					
			//查找对应的字符串的值
			foreach($array as $kk => $vv){
				foreach($tags as $kkk => $vvv){
					if($vv==$vvv['id']){
						$tags2[$count][]=$vvv['name'];
					}
				}
			}
			$count++;
		}
		
		#echo "<pre>";
		#var_dump($list);
		#var_dump($string);
		#var_dump($array);
		#var_dump($tags);
		#var_dump($tags2);
		#echo "</pre>";
		#die;
		
		$this->assign('tags2',$tags2);
		
		
		
        $this->assign("list",$list);
        $this->assign("sum",$sum);

        

        //查找评论
        $db = M('comments');
        $comments = $db->where("type=0")->order('time asc')->select();
        // var_dump($comments);die;
        $this->assign("comments",$comments);
        $this->display();
    }

    //增加评论
    public function addcomment(){
        $data = array(
            "parent"=>I('parent'),
            "belong" => I("id"),
            "content" => I("comment"),
            "username" => session("username")?session("username"):"游客",
            "time" => I('time'),
            "email" => I("email")?I("email"):NULL,
            "type" => 0
        );
        $db = M('comments');
        // var_dump($db->save($data));
        $flag = $db->data($data)->add();

        if(!$flag){
            $this->error("失败",U('Index/index'));
        }else{
            M('list')->where(array("id"=>I("id")))->setInc('commentnum');
        }
        //回复其他人的留言
         echo json_encode($data);

        return;
    }
    public function test(){
        //echo date("Y-m-d H:m:s");
        //echo "<br/>";
        //echo date("Y-m-d H:m:s %s",strtotime("now"));
        $this->display();
    }
    //返回评论
    public function recomment(){
        $db = M('comments');
        $id['id'] = I('id');
        $id['type'] = 0;
        $data = $db->where($id)->select();
        return $data;
    }
	//根据标签查找文章类型
	public function findArticle(){
		$tab_db = M('tab');
		$article_db = M('list');
		$title['name'] = I('title');
		#print_r($title);
		#echo '<br/>';
		$title = $tab_db->where($title)->select();
		$title = $title[0]['id'];
		#print_r($title);
		#echo '<br/>';
		//模糊查找
		$arr['type'] = array('like',"%$title%");
		#print_r($arr);
		#echo '<br/>';
		#die;
		$data = $article_db->where($arr)->select();
		$this->assign('type',I('title'));
		$this->assign('list',$data);
		$this->display();
	}
	
}